package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.CommentDao;
import com.util.Log;

@WebServlet(urlPatterns={"/admin/ManageComment"})
public class ManageComment extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	private static final String COMMENT_PASS = "COMMENT_PASS";
	private static final String COMMENT_FAIL = "COMMENT_FAIL";

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		String manageType = request.getParameter("manageType");
		String commentId = request.getParameter("commentId");
		String message = "留言管理失败";
		Boolean daoResult = false;
		
		if(!manageType.equals("") && !commentId.equals("")) {
			CommentDao dao = new CommentDao();
			
			if(manageType.equals(COMMENT_PASS)) {
				daoResult = dao.updateCommentVeriryResult(Integer.parseInt(commentId), 1);
			} else if(manageType.equals(COMMENT_FAIL)) {
				daoResult = dao.updateCommentVeriryResult(Integer.parseInt(commentId), -1);
			}
			
			if (daoResult)
				message = "留言管理操作成功";
			dao.closeAll();
		} else {
			message = "留言管理请求参数错误";
		}

		String json = "{\"status\":\"" + message + "\"}";
		Log.write(json);
		out.println(json);
		out.flush();
		out.close();
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
